// 导入依赖的模块
yepjs.require("jquery");
yepjs.require("YBA.Common");
yepjs.require("YBA.Plugin.plugins");
yepjs.require("YBA.Data.User");


// 用户管理 - 个人信息查询/修改
yepjs.define("detali", {

    NS: "YBA.Logic.User",

    field: function() {

        var Biz = {
            // 初始化事件
            init: function() {
                Biz.fn.getUserDetail(Biz.vars.data,function (data) {
                    Biz.fn.rendeDetail(data);
                });
                for (var i in Biz.event) {
                    Biz.event[i]();
                }
            },
            //  公共变量
            vars: {
                // 模板列表
                tpl: (function(){
                    var tpl = {};
                    $("script[type='text/ysmart']").each(function(){
                        var t = $(this);
                        tpl[t.attr("data-name")] = t.html();
                    });
                    return tpl;
                })(),
                //发起请求当前用户信息时所需当前用户id，用户id应该对前端隐藏  ，暂发起请求不传递参数
                data:{
                	"userId":$("#onlineUserId").val()
                }
            },
            // 页面元素
            el: {
                jinfoBlock:$("#j-infoBlock"),
                jeditInfoBtn:$("#j-editInfoBtn"),
                jeditPasswordBtn:$("#j-editPasswordBtn")
            },
            // 功能函数集合
            fn: {
                // 渲染个人信息
                rendeDetail: function (data) {
                    Biz.el.jinfoBlock.html(YBA.Common.ysmart(Biz.vars.tpl.infoTpl, data || {}));
                },
                // 获取用户详情
                getUserDetail: function(data, callback){
                    YBA.Data.User.getUserDetail(data, function(json){
                        if(json.code == 200) {
                            callback && callback(json.obj);
                        }else{
                            YBA.Plugin.msgtip({type: "i", msg: json.desc})
                        }
                    })
                },
                // 表单验证
                formCheck: function(form,name) {
                	// 显示错误信息
                    var showError = function(obj, msg) {
                        var msgDom = obj.siblings('.message');
                        var msgTxt = msgDom.children('i')
                        msgDom.addClass("msg-error").removeClass('msg-tip');
                        msgTxt.html(msg);
                        msgDom.show();
                    };

                    // 隐藏错误信息
                    var hideError = function(obj, msg) {
                        var msgDom = obj.siblings('.message');
                        var msgTxt = msgDom.children('i')
                        msgDom.hide();
                        msgDom.addClass("msg-tip").removeClass('msg-error');
                        msgTxt.html(msg);
                    };

                    // 数据验证对象
                    var dataCheck = {
                        newPassword:{
                            obj: form.find("[name='newPassword']"),
                            fn: function() {
                                var val = this.obj.val();
                                if (YBA.Common.check.isNull(val)) {
                                    showError(this.obj,"请输入新密码");
                                    return false;
                                }else if(!YBA.Common.check.isPwd(val)){
                                	showError(this.obj,"至少六位的英文数字组合");
                                    return false;
                                }else if(YBA.Common.check.isHowLong(val,50)){
                                	showError(this.obj,"新密码过长");
                                    return false;
                                } else {
                                    hideError(this.obj,"请输入新密码");
                                    return true;
                                }
                            }
                        },
                        passwordSecond: {
                            obj: form.find("[name='passwordSecond']"),
                            fn: function() {
                                var val = this.obj.val();
                                if (YBA.Common.check.isNull(val)) {
                                    showError(this.obj,"请再次输入新密码");
                                    return false;
                                } else if(val != dataCheck["newPassword"].obj.val()){
                                    showError(this.obj,"两次输入的密码不一致");
                                    return false;
                                }else {
                                    hideError(this.obj,"请再次输入新密码");
                                    return true;
                                }
                            }
                        },
                        oldPassword: {
                            obj: form.find("[name='oldPassword']"),
                            fn: function() {
                                if (this.obj.length <= 0) {return true;};
                                var val = this.obj.val();
                                if (YBA.Common.check.isNull(val)) {
                                    showError(this.obj,"请输入旧密码");
                                    return false;
                                } else if(YBA.Common.check.isHowLong(val,50)){
                                	showError(this.obj,"旧密码过长");
                                    return false;
                                } else {
                                    hideError(this.obj,"请输入旧密码");
                                    return true;
                                }
                            }
                        },
                        mobile: {
                            obj: form.find("[name='mobile']"),
                            fn: function() {
                                var val = this.obj.val();
                                if (YBA.Common.check.isNull(val)) {
                                    showError(this.obj,"请输入手机号");
                                    return false;
                                } if (!YBA.Common.check.isMobile(val)) {
                                    showError(this.obj,"手机号不合法");
                                    return false;
                                } else {
                                    hideError(this.obj,"请输入手机号");
                                    return true;
                                }
                            }
                        },
                        email: {
                            obj: form.find("[name='email']"),
                            fn: function() {
                                var val = this.obj.val();
                                if (YBA.Common.check.isNull(val)) {
                                    showError(this.obj,"请输入邮箱");
                                    return false;
                                } if (!YBA.Common.check.isEmail(val)) {
                                    showError(this.obj,"邮箱不合法");
                                    return false;
                                } else {
                                    hideError(this.obj,"请输入邮箱");
                                    return true;
                                }
                            }
                        }
                    };

                    if (name && dataCheck[name]) {
                        return dataCheck[name].fn();
                    } else {
                        var errLen = 0;
                        for (var i in dataCheck) {
                            if (!dataCheck[i].fn()) {
                                errLen++;
                            }
                        }
                        if (errLen > 0) {
                            return false;
                        }
                        return true;
                    }
                },
                //用户联系信息修改
                editInfoForm:function(data){
                    var popform = new YBA.Plugin.popform({
                        title:"修改联系信息",
                        className:"ui-dialog-userInfo",
                        content:YBA.Common.ysmart(Biz.vars.tpl.editInfoTpl,data || {}),
                        width:564,
                        destroy:true,
                        onHide: function () {
                            // 隐藏弹窗时要执行的回调函数
                        },
                        onShow:function(){
                            // 显示弹窗时要执行的回调函数
                            var op = $(".form-group");
                            var form = $(".ui-dialog-form");
                            op.each(function() {
                                var t = $(this),
                                    input = t.find(".required"),
                                    name = input.attr("name"),
                                    holder = input.siblings(".message");
                                input.blur(function() {
                                    Biz.fn.formCheck(form,name);
                                }).focus(function() {
                                    holder.show();
                                });;
                            });
                        },
                        btns: [{
                            value: "确定",
                            className: "blue btn g-mr10",
                            fn: function () {
                                var form = $(".ui-dialog-form");
                                if (!(Biz.fn.formCheck(form,"mobile")&Biz.fn.formCheck(form,"email"))) {
                                    return false;
                                }else{
                                    var form = popform.self.find("form");
                                    var data = form.serialize();

                                    YBA.Data.User.updateUser(data, function(json){
                                        if(json.code == 200) {
                                            YBA.Plugin.msgtip({type: "o", msg: json.desc});
                                            popform.hide();
                                            Biz.fn.getUserDetail(Biz.vars.data,function (data) {
                                                Biz.fn.rendeDetail(data);
                                            });
                                        }else{
                                            YBA.Plugin.msgtip({type: "i", msg: json.desc});
                                        }
                                    });
                                }
                            }
                        }, {
                            value: "取消",
                            className: "red btn g-ml10",
                            fn: function () {
                                popform.hide();
                            }
                        }]
                    }).show();
                },
                //用户密码修改
                editPasswordForm:function(data){
                    var popform = new YBA.Plugin.popform({
                        title: "修改密码",
                        className: "ui-dialog-editPassword",
                        content: YBA.Common.ysmart(Biz.vars.tpl.editPasswordTpl,data || {}),
                        width: 564,
                        destroy: true,
                        onHide: function () {
                            // 隐藏弹窗时要执行的回调函数
                        },
                        onShow: function () {
                            // 显示弹窗时要执行的回调函数
                            var op = $(".form-group");
                            var form = $(".ui-dialog-form");
                            op.each(function() {
                                var t = $(this),
                                    input = t.find(".required"),
                                    name = input.attr("name"),
                                    holder = input.siblings(".message");
                                input.blur(function() {
                                    Biz.fn.formCheck(form,name);
                                }).focus(function() {
                                    holder.show();
                                });;
                            });
                        },
                        btns: [{
                            value: "确定",
                            className: "blue btn g-mr10",
                            fn: function () {
                                var form = $(".ui-dialog-form");
                                if (!(Biz.fn.formCheck(form,"oldPassword")&Biz.fn.formCheck(form,"newPassword")&Biz.fn.formCheck(form,"passwordSecond"))) {
                                    return false;
                                }else{
                                    var form = popform.self.find("form");
                                    var data = form.serialize();
                                    
                                    data = data + "&userId="+ $("#onlineUserId").val();


                                    YBA.Data.User.updateUserPassword(data, function(json){
                                        if(json.code == 200) {
                                            YBA.Plugin.msgtip({type: "o", msg: json.desc});
                                            popform.hide();
                                            Biz.fn.getUserDetail(Biz.vars.data,function (data) {
                                                Biz.fn.rendeDetail(data);
                                            });
                                        }else{
                                            YBA.Plugin.msgtip({type: "i", msg: json.desc});
                                        }
                                    });
                                }
                            }
                        }, {
                            value: "取消",
                            className: "btn red g-ml10",
                            fn: function () {
                                popform.hide();
                            }
                        }]
                    }).show();
                }
            },
            // 初始化时执行的所有事件
            event: {
                // 用户个人联系信息修改
                editInfo: function(){
                    Biz.el.jeditInfoBtn.bind("click", function(){
                        Biz.fn.getUserDetail(Biz.vars.data, function(data){
                            Biz.fn.editInfoForm(data);
                        });
                    });
                },
                // 用户个人密码修改
                editPassword: function(){
                    Biz.el.jeditPasswordBtn.bind("click", function(){
                        Biz.fn.getUserDetail(Biz.vars.data, function(data){
                            Biz.fn.editPasswordForm(data);
                        });
                    });
                }
            }
        };

        Biz.init();

        return Biz;
    }
});
